using Mono.Cecil.Cil;

namespace Cil.Ast;

public sealed class VariableGetExpression : SingleInstructionExpression
{
	const int MaximumVariablesAlloved = 65534;

	public VariableGetExpression(Context context, Instruction position, VariableReference ref_variable)
		: base(context, position)
	{   
		if(ref_variable.Index >= MaximumVariablesAlloved) 
		{
			throw new ArgumentOutOfRangeException(string.Format("Method must not contain more than {0} variables. Please, optimize your code.", MaximumVariablesAlloved));
		}

		// base class
		ReadsStack = 0;
		WritesStack = 1;
		Nodes = new FixedList<Expression>();

		// this
		VariableReference = ref_variable;
		ReturnType = VariableReference.VariableType;
	}

	public VariableReference VariableReference { get; private set; }

	public bool IsCatchVariable { get { return false; } }

	public override string ToString()
	{
		return string.Format("{0}", VariableReference);
	}
}

public static partial class IlAst
{
	public static VariableGetExpression VariableGet(Context context, Instruction position, VariableReference ref_variable)
	{
		return new VariableGetExpression(context, position, ref_variable);
	}
}